package cn.java81.jdbc;


import java.sql.*;

public class Demo1 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
        String url = "jdbc:mysql://localhost:3308/myschool81?useSSL=false&serverTimezone=Asia/Shanghai";
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            con = DriverManager.getConnection(url, "root", "root1234");
            st = con.createStatement();
            //结果集在第一行之前
            rs = st.executeQuery("select * from s_subject");
            while (rs.next()) {
                //数据库类型和java的对应
                long id = rs.getLong("id");
                long gid = rs.getLong("gradeid");
                String subjname = rs.getString("subjectname");
                int hours = rs.getInt("classhour");
                System.out.println("id:" + id + ",gid=" + gid + ",课程名：" + subjname + ",学时：" + hours);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
